package com.zxc.judgeservice.service.impl;


import cn.hutool.json.JSONUtil;
import com.zxc.common.ErrorCode;
import com.zxc.common.exception.BusinessException;
import com.zxc.judgeservice.codesandbox.CodeSandbox;
import com.zxc.judgeservice.codesandbox.CodeSandboxFactory;
import com.zxc.judgeservice.codesandbox.CodeSandboxProxy;
import com.zxc.judgeservice.mapper.JudgeManager;
import com.zxc.judgeservice.service.JudgeService;
import com.zxc.judgeservice.startegy.JudgeContext;
import com.zxc.oj.model.codesandbox.ExecuteCodeRequest;
import com.zxc.oj.model.codesandbox.ExecuteCodeResponse;
import com.zxc.oj.model.codesandbox.JudgeInfo;
import com.zxc.oj.model.dto.question.JudgeCase;
import com.zxc.oj.model.entity.Question;
import com.zxc.oj.model.entity.QuestionSubmit;
import com.zxc.oj.model.enums.QuestionSubmitStatusEnum;

import com.zxc.service.QuestionFeignClient;

import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Log
@Service
@RequiredArgsConstructor
public class JudgeServiceImpl implements JudgeService {
    @Value("${sandbox.type}")
    private String type;
    private final QuestionFeignClient questionFeignClient;


    @Override
    public QuestionSubmit doJudge(long questionSubmitId) {

        QuestionSubmit questionSubmit = questionFeignClient.getQuestionSubmitById(questionSubmitId);
        if (questionSubmit == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "提交信息不存在");
        }
        Long questionId = questionSubmit.getQuestionId();
        Question question = questionFeignClient.getQuestionById(questionId);
        String value = questionSubmit.getLanguage().getValue();
        log.info("questionSubmit:"+value);
        if (question == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "问题不存在");
        }

        if (!questionSubmit.getStatus().equals(QuestionSubmitStatusEnum.WAITING.getValue())) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "提交信息不合法");
        }

        QuestionSubmit questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.RUNNING.getValue());
        boolean update = questionFeignClient.updateQuestionSubmitById(questionSubmitUpdate);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "题目状态更新错误");
        }

        CodeSandbox codeSandbox = CodeSandboxFactory.buildCodeSandbox(type);
        CodeSandboxProxy codeSandboxProxy = new CodeSandboxProxy(codeSandbox);
        String judgeCaseStr = question.getJudgeCase();
        List<JudgeCase> judgeCases = JSONUtil.toList(judgeCaseStr, JudgeCase.class);
        List<String> inputs = judgeCases.stream().map(JudgeCase::getInput).collect(Collectors.toList());
        ExecuteCodeRequest executeCodeRequest = ExecuteCodeRequest.builder().inputList(inputs).language(questionSubmit.getLanguage()).code(questionSubmit.getCode()).build();
        ExecuteCodeResponse executeCodeResponse = codeSandboxProxy.executeCode(executeCodeRequest);
        List<String> outputs = executeCodeResponse.getOutputList();

        //使用策略模式
        JudgeContext judgeContext = new JudgeContext();
        judgeContext.setJudgeInfo(executeCodeResponse.getJudgeInfo());
        judgeContext.setInputs(inputs);
        judgeContext.setOutputs(outputs);
        judgeContext.setQuestion(question);
        judgeContext.setJudgeCases(judgeCases);
        judgeContext.setQuestionSubmit(questionSubmit);
        JudgeInfo judgeInfo = new JudgeManager().doJudge(judgeContext);
        //更新数据库信息
        questionSubmitUpdate = new QuestionSubmit();
        questionSubmitUpdate.setId(questionSubmitId);
        questionSubmitUpdate.setStatus(QuestionSubmitStatusEnum.SUCCEED.getValue());
        questionSubmitUpdate.setJudgeInfo(JSONUtil.toJsonStr(judgeInfo));
        update = questionFeignClient.updateQuestionSubmitById(questionSubmitUpdate);
        if (!update) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "题目状态更新错误");
        }
        //查询题目状态
        questionSubmit = questionFeignClient.getQuestionSubmitById(questionSubmitId);
        return questionSubmit;
    }
}
